using ASP;
using HoWave.Common;
using HoWave.Web;
using HoWave.Web.Shop.Member;
using System;
using System.Data;
using System.IO;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class Member_Shop_GetProduct : Member, IRequiresSessionState
{
	protected Label ClassName;
	protected HiddenField ClassID;
	protected HtmlGenericControl ClassBarDiv;
	protected DropDownList dspecal;
	protected CheckBoxList dPayType;
	protected DropDownList dDiverType;
	protected HtmlInputFile FileOfExecl;
	protected Button ButNext;
	private static int UserID = 0;
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		Member_Shop_GetProduct.UserID = base.user.UserID;
		if (!base.IsPostBack)
		{
			this.BadnSpecail();
			this.BandDevliver();
			this.BandPayType();
		}
	}
	private void BandDevliver()
	{
		this.dDiverType.DataSource = new Shop_DeliverType().GetDeliverTypeByUserID(Member_Shop_GetProduct.UserID);
		this.dDiverType.DataTextField = "TypeName";
		this.dDiverType.DataValueField = "TypeID";
		this.dDiverType.DataBind();
	}
	private void BandPayType()
	{
		this.dPayType.DataSource = new Shop_PayType().GetUnDisabledPayTypeByUserID(Member_Shop_GetProduct.UserID);
		this.dPayType.DataTextField = "TypeName";
		this.dPayType.DataValueField = "TypeID";
		this.dPayType.DataBind();
	}
	private void BadnSpecail()
	{
		this.dspecal.DataSource = new SpecialBll().GetAllSpcilNameID();
		this.dspecal.DataTextField = "SpecialName";
		this.dspecal.DataValueField = "SpecialID";
		this.dspecal.DataBind();
	}
	protected void ButNext_Click(object sender, EventArgs e)
	{
		if (this.FileOfExecl.PostedFile.FileName == null || this.FileOfExecl.PostedFile.FileName == "")
		{
			base.Response.Write("<script>alert('请选择execl文件')</script>");
		}
		else
		{
			if (string.IsNullOrEmpty(this.ClassID.Value))
			{
				base.Response.Write("<script>alert('请选择类目')</script>");
			}
			else
			{
				if (this.dDiverType.SelectedValue == "0")
				{
					base.Response.Write("<script>alert('请选择物流方式')</script>");
				}
				else
				{
					string text = "";
					int num = 0;
					foreach (ListItem listItem in this.dPayType.Items)
					{
						if (listItem.Selected)
						{
							if (text == "")
							{
								text += listItem.Value;
							}
							else
							{
								text = text + listItem.Value + ",";
							}
							num++;
						}
					}
					if (num == 0)
					{
						base.Response.Write("<script>alert('请选择付款方式')</script>");
					}
					else
					{
						if (text.IndexOf(",") > -1)
						{
							text = text.Remove(text.LastIndexOf(","));
						}
						string fileName = Path.GetFileName(this.FileOfExecl.PostedFile.FileName);
						string text2 = base.Server.MapPath(".").ToLower().Replace("hl_admin", "") + "\\" + fileName;
						this.FileOfExecl.PostedFile.SaveAs(text2);
						int num2 = text2.LastIndexOf("\\");
						string path = text2.Substring(0, text2.LastIndexOf("\\"));
						string extension = Path.GetExtension(text2);
						if (extension == ".csv" || extension == ".xls")
						{
							DataTable dataTable = new DataTable();
							DataSet dataSet = new DataSet();
							if (extension == ".csv")
							{
								ReadExcel readExcel = new ReadExcel(path, fileName);
								dataSet = readExcel.Csv();
							}
							else
							{
								ReadExcel readExcel = new ReadExcel(text2);
								dataSet = readExcel.Execl();
							}
							if (dataSet == null || dataSet.Tables.Count == 0)
							{
								base.Response.Write("<script>alert('无法正确读取execel文件')</script>");
								return;
							}
							dataTable = dataSet.Tables[0];
							try
							{
								int classID = int.Parse(this.ClassID.Value);
								int shopID = base.user.ShopID;
								string userName = base.user.UserName;
								DataTable deliverTypeByUserIDTypeID = new Shop_DeliverType().GetDeliverTypeByUserIDTypeID(Member_Shop_GetProduct.UserID, this.dDiverType.SelectedValue);
								decimal num3 = decimal.Parse(deliverTypeByUserIDTypeID.Rows[0]["Charge"].ToString());
								int deliverPayType = int.Parse(deliverTypeByUserIDTypeID.Rows[0]["ChargeType"].ToString());
								int num4 = int.Parse(this.dDiverType.SelectedValue);
								if (base.user.CheckGroupPower("allowsaleproduct"))
								{
									int num5 = Text.ToInt32(base.user.GetGroupPower("maxproduct"));
									if (num5 > 0)
									{
										Shop_Product shop_Product = new Shop_Product(this.conn);
										if (num5 <= shop_Product.CountUserProductNum(base.user.UserID) + dataTable.Rows.Count)
										{
											base.Response.Write("<script>alert('导入商品超过允许数量')</script>");
											return;
										}
									}
								}
								int num6 = 0;
								for (int i = 0; i < dataTable.Rows.Count; i++)
								{
									DataRow dataRow = dataTable.Rows[i];
									if (dataRow == null || dataRow["库存量"] == null)
									{
										break;
									}
									bool autoPostBack = dataRow["自动重发"].ToString() == "1";
									int num7 = int.Parse(dataRow["库存量"].ToString());
									decimal auctionStartPrice = decimal.Parse(dataRow["起拍价"].ToString());
									DateTime beginDate = DateTime.Parse(dataRow["开始日期"].ToString());
									int buyTimes = 0;
									int channelID = int.Parse(this.dspecal.SelectedValue);
									string city = dataRow["所在城市"].ToString();
									DateTime now = DateTime.Now;
									decimal currentPrice = decimal.Parse(dataRow["一口价"].ToString());
									int discount = int.Parse(dataRow["折扣"].ToString());
									int discount2 = 0;
									int discount3 = 0;
									int discount4 = 0;
									bool enableSale = false;
									bool enableSingleSell = false;
									DateTime endDate = DateTime.Parse(dataRow["结束日期"].ToString());
									bool guarantee = dataRow["是否有保修"].ToString() == "1";
									int hits = 0;
									decimal imcrementPrice = decimal.Parse(dataRow["加价幅度"].ToString());
									bool isElite = false;
									bool isHot = false;
									string keyword = dataRow["关键字"].ToString();
									int limitNum = int.Parse(dataRow["限制购买量"].ToString());
									decimal memberPrice = 0m;
									if (dataTable.Columns.Contains("参考价"))
									{
										if (dataRow["参考价"] != DBNull.Value)
										{
											memberPrice = decimal.Parse(dataRow["参考价"].ToString());
										}
									}
									bool needInvoice = dataRow["是否开发票"].ToString() == "1";
									bool onTop = false;
									int presentExp = 0;
									int presentExp2 = 0;
									int presentExp3 = 0;
									int presentExp4 = 0;
									int value = 0;
									int value2 = 0;
									int value3 = 0;
									int value4 = 0;
									int presentPoint = 0;
									int presentPoint2 = 0;
									int presentPoint3 = 0;
									int presentPoint4 = 0;
									string producerName = dataRow["生产者名称"].ToString();
									string productImageUrl = dataRow["商品图片"].ToString();
									string productIntro = dataRow["商品介绍"].ToString();
									string productModel = "";
									string productName = dataRow["商品名称"].ToString();
									string productNum = dataRow["商品编号"].ToString();
									string productPlace = dataRow["商品所在地"].ToString();
									string productSmallImageUrl = dataRow["商品略缩图"].ToString();
									string productStandard = dataRow["商品属性"].ToString();
									int num8 = int.Parse(dataRow["商品类型"].ToString());
									string propority = "";
									string province = dataRow["所在省份"].ToString();
									string pUnit = dataRow["单位"].ToString();
									string remark = dataRow["备注"].ToString();
									int salePromotionType = 0;
									int userID = Member_Shop_GetProduct.UserID;
									int skinID = 0;
									int validDay = int.Parse(dataRow["有效期"].ToString());
									decimal standPrice = decimal.Parse(dataRow["一口价"].ToString());
									int stars = 0;
									int stocks = int.Parse(dataRow["库存量"].ToString());
									decimal taxRate = decimal.Parse(dataRow["税率"].ToString());
									int num9 = int.Parse(dataRow["团购1当前参加数目"].ToString());
									int num10 = int.Parse(dataRow["团购2当前参加数目"].ToString());
									int num11 = int.Parse(dataRow["团购3当前参加数目"].ToString());
									int num12 = int.Parse(dataRow["团购4当前参加数目"].ToString());
									int num13 = int.Parse(dataRow["团购5当前参加数目"].ToString());
									string teamName = dataRow["团购名称"].ToString();
									int teamNum = int.Parse(dataRow["团购档次"].ToString());
									int teamNumber = int.Parse(dataRow["团购1最低参团数目"].ToString());
									int teamNumber2 = int.Parse(dataRow["团购2最低参团数目"].ToString());
									int teamNumber3 = int.Parse(dataRow["团购3最低参团数目"].ToString());
									int teamNumber4 = int.Parse(dataRow["团购4最低参团数目"].ToString());
									int teamNumber5 = int.Parse(dataRow["团购5最低参团数目"].ToString());
									decimal teamPrice = decimal.Parse(dataRow["团购价1"].ToString());
									decimal teamPrice2 = decimal.Parse(dataRow["团购价2"].ToString());
									decimal teamPrice3 = decimal.Parse(dataRow["团购价3"].ToString());
									decimal teamPrice4 = decimal.Parse(dataRow["团购价4"].ToString());
									decimal teamPrice5 = decimal.Parse(dataRow["团购价5"].ToString());
									int templateID = 0;
									decimal tempSubScriptionRate = decimal.Parse(dataRow["团购预付金比例"].ToString());
									string trademarkName = dataRow["商品品牌"].ToString();
									DateTime now2 = DateTime.Now;
									string uploadFiles = "";
									int weight = int.Parse(dataRow["重量"].ToString());
									new Shop_Product().AddProduct(shopID, Member_Shop_GetProduct.UserID, channelID, productPlace, productNum, classID, productName, productModel, productStandard, producerName, trademarkName, productIntro, productImageUrl, num8.ToString(), standPrice, auctionStartPrice, imcrementPrice, memberPrice, currentPrice, teamName, teamNum, teamPrice, tempSubScriptionRate, teamNumber, teamPrice2, teamNumber2, teamPrice3, teamNumber3, teamPrice4, teamNumber4, teamPrice5, teamNumber5, isElite, stocks, enableSale, keyword, userName, salePromotionType, weight, propority, num4.ToString(), num3.ToString(), needInvoice, guarantee, validDay, autoPostBack, remark, deliverPayType, text, text, hits, buyTimes, onTop, isHot, stars, beginDate, endDate, limitNum, enableSingleSell, templateID, skinID, discount, presentExp, presentPoint, value, discount2, presentExp2, presentPoint2, value2, discount3, presentExp3, presentPoint3, value3, discount4, presentExp4, presentPoint4, value4, pUnit, "", productSmallImageUrl, taxRate, uploadFiles, province, city, "", "", "", 0m, "默认", "默认", "", false);
									num6++;
								}
								base.Response.Write("<script>alert('数据出入成功，共导入" + num6.ToString() + "条记录，请核查')</script>");
							}
							catch (Exception ex)
							{
								base.Response.Write("<script>alert('数据转换失败，可能电子表格的数据格式不正确，请检查下面的原因：" + ex.Message.ToString() + "')</script>");
								return;
							}
						}
						else
						{
							base.Response.Write("<script>alert('文件格式不正确')</script>");
						}
						File.Delete(text2);
					}
				}
			}
		}
	}
}
